#!/usr/bin/env python

# Original bug script from Piotr Kaleta (pkaleta)

# The essential issue is that build_cigar_string overwrites cached
# data.

import hgvs.dataproviders.uta
import hgvs.location
import hgvs.posedit
import hgvs.edit
import hgvs.sequencevariant
import hgvs.variantmapper
from support import CACHE

def create_genomic_variant():
    start = hgvs.location.SimplePosition(118898437)
    end = hgvs.location.SimplePosition(118898437)
    iv = hgvs.location.Interval(start=start, end=end)
    edit = hgvs.edit.NARefAlt(ref='G', alt='T')
    posedit = hgvs.posedit.PosEdit(pos=iv, edit=edit)
    genomic_variant = hgvs.sequencevariant.SequenceVariant(ac='NC_000011.9', type='g', posedit=posedit, )
    return genomic_variant


gvar = create_genomic_variant()

hdp = hgvs.dataproviders.uta.connect(mode="run", cache=CACHE)

print("build a variant mapper once for the given transcript: all okay")
variantmapper = hgvs.variantmapper.VariantMapper(hdp)
for _ in range(4):
    print(variantmapper.g_to_c(gvar, 'NM_001164277.1'))

print("""build a variant mapper in each loop. Although this is wasteful, it
should return consistent results. It doesn't.""")
for _ in range(4):
    variantmapper = hgvs.variantmapper.VariantMapper(hdp)
    print(variantmapper.g_to_c(gvar, 'NM_001164277.1'))

# As of 1b1d5ff268e3 (Feb 2015), one gets:
# (default-2.7)snafu$ ./tests/bug-examples/220 
## build a variant mapper once for the given transcript: all okay
## NM_001164277.1:c.526+1C>A
## NM_001164277.1:c.526+1C>A
## NM_001164277.1:c.526+1C>A
## NM_001164277.1:c.526+1C>A
## build a variant mapper in each loop. Although this is wasteful, it
## should return consistent results. It doesn't.
## NM_001164277.1:c.526C>A
## NM_001164277.1:c.526+1C>A
## NM_001164277.1:c.526C>A
## NM_001164277.1:c.526+1C>A
